package com.forlove.app.qselect.answer.widght;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.view.Display;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.forlove.app.ForloveApplication;
import com.forlove.app.R;
import com.forlove.app.common.network.ICallBack;
import com.forlove.app.common.network.OkhttpManager;
import com.forlove.app.common.utils.LogUtil;
import com.forlove.app.common.utils.ParseHelper;
import com.forlove.app.common.utils.ShareUtils;
import com.forlove.app.common.utils.StringUtils;
import com.forlove.app.manager.ServerManager;
import com.forlove.app.manager.UserManager;
import com.forlove.app.qselect.answer.entity.InviteShortLinkEntity;
import com.forlove.app.qselect.login.entity.UserEntity;
import com.umeng.socialize.bean.SHARE_MEDIA;

import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 邀请好友
 * Created by yjl on 2017/10/12.
 */

public class InviteFriendDialog extends Dialog implements View.OnClickListener {

    private ImageView weixinFriends;
    private ImageView weixinOnline;
    private ImageView weibo;
    private ImageView qqFriends;
    private ImageView qqZone;
    private LinearLayout shareSMS;
    private LinearLayout shareOther;
    private Window window;
    private TextView bt_cancel;
    private Context mContext;
    private String mUrl;
    private String title;
    private String sharDescription;//描述
    private String wxShareTile;
    private String wxShareDesc;
    private String wxUrl;
    private String qqUrl;
    private String qqZoneUrl;
    private String wburl;
    private String wxCircleShareTile;
    private String wxCircleUrl;
    private String commonUrl;
    private String messText;
    private SHARE_MEDIA[] media = {SHARE_MEDIA.SINA, SHARE_MEDIA.QQ,
            SHARE_MEDIA.QZONE, SHARE_MEDIA.WEIXIN, SHARE_MEDIA.WEIXIN_CIRCLE, SHARE_MEDIA.SMS};

    public InviteFriendDialog(Context context, String url, String title, String sharDescription) {
        super(context, R.style.ShareDialogStyle);
        this.mContext = context;
        this.mUrl = url;
        this.title = title;
        this.sharDescription = sharDescription;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_invist_friend);
        window = getWindow();
        WindowManager.LayoutParams params = window.getAttributes();
        params.gravity = Gravity.BOTTOM;
        WindowManager windowManager = ((Activity) mContext).getWindowManager();
        Display display = windowManager.getDefaultDisplay();
        params.width = (int) (display.getWidth());
        window.setAttributes(params);
        weixinFriends = (ImageView) findViewById(R.id.icon_weixin_friends);
        weixinOnline = (ImageView) findViewById(R.id.icon_weixin_online);
        weibo = (ImageView) findViewById(R.id.icon_weibo);
        qqFriends = (ImageView) findViewById(R.id.icon_qq_friends);
        qqZone = (ImageView) findViewById(R.id.icon_qqzone);
        bt_cancel = (TextView) findViewById(R.id.bt_cancel);
        shareSMS = findViewById(R.id.ll_share_mess);
        shareOther = findViewById(R.id.ll_share_other);
        setOnclickListener();
        if (!StringUtils.isNotEmpty(mUrl)) {
            getShortLink(commonUrl);
        }
    }

    private void setOnclickListener() {
        weixinFriends.setOnClickListener(this);
        weixinOnline.setOnClickListener(this);
        weibo.setOnClickListener(this);
        qqFriends.setOnClickListener(this);
        qqZone.setOnClickListener(this);
        bt_cancel.setOnClickListener(this);
        shareSMS.setOnClickListener(this);
        shareOther.setOnClickListener(this);
        initShareData();
    }

    private void initShareData() {
        //微信好友
        UserEntity userEntity = UserManager.getUesrInfo(ForloveApplication.getInstance());
        commonUrl = ServerManager.getServerUrl() + "/invtepage?username=" + URLEncoder.encode(userEntity.userName);
        wxShareTile = "来自" + userEntity.getAvailableName() + "的圈一圈邀请";//微信好友分享Title
        wxShareDesc = "哈哈，好友邀请你加入“圈一圈”，来看看身边人对自己的印象吧。";//微信分享好友描述
        wxUrl = "https://t.growingio.com/app/at0/La9BGrRn";//微信好友url
        //微信朋友圈
        wxCircleShareTile = userEntity.getAvailableName() + "邀请你上圈一圈匿名投票，发现朋友眼中的优点";//微信朋友圈分享Title
        //String wxCircleShareDesc="哈哈，好友邀请你加入“圈一圈”，来看看身边人对自己的印象吧。";//微信分享朋友圈描述
        wxCircleUrl = "https://t.growingio.com/app/at0/DnRbMz93";//微信朋友圈url

        qqUrl = "https://t.growingio.com/app/at0/MQReX39q";//qqurl
        qqZoneUrl = "https://t.growingio.com/app/at0/AwoVxkR2";//qq空间url
        wburl = "https://t.growingio.com/app/at0/nxogWvom";//微博url
        messText = userEntity.getAvailableName() + "邀请你来玩“圈一圈”，看看谁喜欢你";
    }


    public void setShareUrl(String shareUrl) {
        if (StringUtils.isNotEmpty(shareUrl)) {
            this.mUrl = shareUrl;
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.icon_weibo:
                if (isWxInstall(mContext)) {
                    ShareUtils.toSharedWX(media[0], (Activity) mContext, mUrl, wxShareTile, wxCircleShareTile + "@圈一圈朋友互动");
                } else {
                    Toast.makeText(mContext, "没有安装相关应用", Toast.LENGTH_SHORT).show();
                }
                dismiss();
                break;
            case R.id.icon_qq_friends:
                ShareUtils.toSharedWX(media[1], (Activity) mContext, mUrl, wxShareTile, wxShareDesc);
                dismiss();
                break;
            case R.id.icon_qqzone:
                ShareUtils.toSharedWX(media[2], (Activity) mContext, mUrl, wxCircleShareTile, wxShareDesc);
                dismiss();
                break;
            case R.id.icon_weixin_friends:
                ShareUtils.toSharedWX(media[3], (Activity) mContext, mUrl, wxShareTile, wxShareDesc);
                dismiss();
                break;
            case R.id.icon_weixin_online:
                ShareUtils.toSharedWX(media[4], (Activity) mContext, mUrl, wxCircleShareTile, wxShareDesc);
                dismiss();
                break;

            case R.id.bt_cancel:
                dismiss();
                break;
            case R.id.ll_share_mess:
                if (StringUtils.isNotEmpty(mUrl)) {
                    ShareUtils.toSharedWX(media[5], (Activity) mContext, mUrl, messText, wxShareDesc);
                } else {
                    ShareUtils.toSharedWX(media[5], (Activity) mContext, commonUrl, messText, wxShareDesc);
                }
                dismiss();
                break;
            case R.id.ll_share_other:
                Intent intent = new Intent();
                intent.setAction(Intent.ACTION_SEND);
                if (StringUtils.isNotEmpty(mUrl)) {
                    intent.putExtra(Intent.EXTRA_TEXT, wxCircleShareTile + mUrl);
                } else {
                    intent.putExtra(Intent.EXTRA_TEXT, wxCircleShareTile + commonUrl);
                }
                intent.setType("text/plain");
                //设置分享列表的标题，并且每次都显示分享列表
                mContext.startActivity(Intent.createChooser(intent, "分享到"));
                dismiss();
                break;
            default:
                break;

        }
    }

    public static boolean isWxInstall(Context context) {
        final PackageManager packageManager = context.getPackageManager();// 获取packagemanager
        List<PackageInfo> pinfo = packageManager.getInstalledPackages(0);// 获取所有已安装程序的包信息
        if (pinfo != null) {
            for (int i = 0; i < pinfo.size(); i++) {
                String pn = pinfo.get(i).packageName;
                if (pn.equals("com.sina.weibo")) {
                    return true;
                }
            }
        }

        return false;
    }

    public void clearRecycle() {
        ShareUtils.clearShare(mContext);
    }

    public void getShortLink(String url) {
        try {
            Map param = new HashMap();
            param.put("url_long", url);
            param.put("source", "445798234");
            OkhttpManager.getInstance().loadDataByPost(ServerManager.URL_SHORT_LINK, param, new ICallBack() {
                @Override
                public void onSuccess(int statusCode, String result) {
                    LogUtil.e("yjl", "onSuccess short link：" + result);
                    try {
                        result = result.replaceAll(" ", "");
                        result = result.replaceAll("\\n", "");
                        List<InviteShortLinkEntity> inviteShortLinkEntity = ParseHelper.getModelList(result, "urls", InviteShortLinkEntity.class);
                        if (inviteShortLinkEntity != null && inviteShortLinkEntity.size() > 0) {
                            mUrl = inviteShortLinkEntity.get(0).url_short;
                        }

                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }

                @Override
                public void onFail(int statusCode, String result) {
                    LogUtil.e("yjl", "onFail short link：" + result);
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
